<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="150px">
      
      <el-form-item :label="$t('deviceDetail.battery.serial')" prop="batterySerial">
        <el-input
          v-model="queryParams.batterySerial"
          :placeholder="$t('deviceDetail.battery.serialPlaceholder')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('deviceDetail.battery.name')" prop="name">
        <el-input
          v-model="queryParams.name"
          :placeholder="$t('deviceDetail.battery.namePlaceholder')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('common.search')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('common.reset')}}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="handleGetBatteryVoltage('')"
        >{{$t('deviceDetail.battery.ObtainVoltage')}}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="handleGetBatteryTemperature('')"
        >{{$t('deviceDetail.battery.ObtainTemperature')}}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['device:deviceBattery:export']"
        >{{$t('common.export')}}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-upload
          class="upload-demo"
          :action="upload"
          :on-success="uploadSuccess"
          :on-error="uploadSuccess"
          :headers="headers"
          :file-list="fileList"
          >
          <el-button type="info" plain icon="el-icon-upload" size="mini" v-hasPermi="['device:deviceBattery:add']">{{$t('common.upload')}}</el-button>
        </el-upload>  
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceBatteryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="$t('deviceDetail.battery.serial')" align="center" prop="batterySerial" />
      <el-table-column :label="$t('deviceDetail.battery.name')" align="center" prop="name" />
      <el-table-column :label="$t('deviceDetail.battery.Voltage')" align="center" prop="voltage" />
      <el-table-column :label="$t('deviceDetail.battery.Temperature')" align="center" prop="temperature" />
      <el-table-column :label="$t('deviceDetail.battery.UpdateTime')" align="center" prop="updateTime" />
      <el-table-column :label="$t('common.operator')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['device:deviceBattery:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-refresh"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['device:deviceBattery:edit']"
          >同步数据</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备电池对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="电池序列号" prop="batterySerial">
          <el-input v-model="form.batterySerial" placeholder="请输入电池序列号" />
        </el-form-item>
        <el-form-item label="设备名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入设备名称" disabled />
        </el-form-item>
        <el-form-item label="电池温度" prop="temperature">
          <el-input v-model="form.temperature" placeholder="请输入设备名称" disabled />
        </el-form-item>
        <el-form-item label="电池电压" prop="voltage">
          <el-input v-model="form.voltage" placeholder="请输入设备名称" disabled />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDeviceBattery, getDeviceBattery, delDeviceBattery, addDeviceBattery, updateDeviceBattery ,getBatteryVoltage,getBatteryTemperature } from "@/api/device/deviceBattery";
import { getToken } from "@/utils/auth";
export default {
  name: "DeviceBattery",
  props:{
    deviceId:'',
    deviceName:''
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备电池表格数据
      deviceBatteryList: [],
      upload:'',
      headers: {
        Authorization: "Bearer " + getToken()
      },
      fileList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceId: null,
        batterySerial: null,
        name: null,
        voltage: null,
        temperature: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceId: [
          { required: true, message: "设备Id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.queryParams.deviceId = this.deviceId
    this.upload = process.env.VUE_APP_BASE_API+'/device/deviceBattery/importData/'+this.deviceId
    this.getList();
  },
  methods: {
    /** 查询设备电池列表 */
    getList() {
      this.loading = true;
      listDeviceBattery(this.queryParams).then(response => {
        this.deviceBatteryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deviceId: this.deviceId,
        batterySerial: null,
        name: null,
        voltage: null,
        temperature: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备电池";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDeviceBattery(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备电池";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDeviceBattery(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeviceBattery(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除设备电池编号为"' + ids + '"的数据项？').then(function() {
        return delDeviceBattery(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('device/deviceBattery/export', {
        ...this.queryParams
      }, `Battery_${this.$attrsdeviceName}.xlsx`)
    },
    uploadSuccess(){
      this.$modal.msgSuccess("上传成功!");
      this.fileList = []
      this.getList()
    },
    uploadError(){
      this.$modal.error("上传失败!");
    },
    //获取电池电压数据
    handleGetBatteryVoltage(id){
      getBatteryVoltage(this.deviceId , id).then( res => this.$modal.msgSuccess("操作成功!"));
    },
    //获取电池温度数据
    handleGetBatteryTemperature(id){
      getBatteryTemperature(this.deviceId , id).then( res => this.$modal.msgSuccess("操作成功!"));
    }   
  }
};
</script>
